<template>
  <el-container>
    <el-header>
      <div class="home-image">
        <img src="../assets/logo.png" alt="" width="50px" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="out">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="clien ">
        <!-- 折叠侧边框 -->
        <div class="collapse-transition" @click="btnSubmenu">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
         class="el-menu-vertical-demo"
         background-color="#333744"
         text-color="#fff"
         active-text-color="#ffd04b"
         unique-opened
         :collapse = "iscode"
         :collapse-transition="false"
         router
         :default-active="seves"
         >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="(item , index) in meunsList" :key="item.id" >
            <!-- 一级菜单的模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsArr[index]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/home'+'/'+childrec.path + ''" v-for="childrec in item.children" :key="childrec.id" @click="seveNavStatus(childrec.path)">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ childrec.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容展示区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 左侧边栏数据
      meunsList: [],
      // 左侧边栏图标
      iconsArr: ['iconfont icon-users', 'iconfont icon-tijikongjian', 'iconfont icon-shangpin', 'iconfont icon-danju', 'iconfont icon-baobiao'],
      iscode: false,
      clien: '200px',
      seves: ''
    }
  },
  methods: {
    out () {
      sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message(res.meta.msg)
      this.meunsList = res.data
    },
    btnSubmenu () {
      this.iscode = !this.iscode
      if (this.iscode) this.clien = '60px'
      if (!this.iscode) this.clien = '200px'
    },
    seveNavStatus (seve) {
      this.seves = '/home/' + seve
    }
  },
  mounted () {
    this.getMenuList()
  }
}
</script>

<style scoped lang="less">
.el-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding: 0;
  align-items: center;
}
.el-aside {
  background-color: #333744;
  .collapse-transition{
    background-color: #4A5064;
    color: #fff;
    letter-spacing: 0.2em;
    cursor: pointer;
    text-align: center;
    height: 50px;
    line-height: 50px;
  }
}
.el-main {
  background-color: #eaedf1;
}
.home-image {
  display: flex;
  align-items: center;
  color: #fff;
  font-size: 20px;
  img {
    padding-right: 15px;
  }
}
.iconfont{
  margin-right: 10px;
  font-size: 18px;
}
.el-menu{
  border: none;
}
</style>
